package net.gdface.sdk;

import java.util.Arrays;
import java.util.LinkedHashMap;
import net.gdface.utils.Assert;

/**
 * 人脸特征1:N比较结果对象<br>
 * 用于保存{@link net.gdface.sdk.FaceApi#compareFaces(byte[], byte[], int)}方法调用的返回结果
 * @author guyadong
 *
 */
public class CompareResult {
	/**
	 * 检测到的人脸信息对象(含特征)
	 */
	CodeInfo[] codes;
	/**
	 * 与codes数组中对应索引指定的人脸特征比较产生的相似度结果
	 */
	double[] similartys;
	public CompareResult() {
		this(new CodeInfo[0], new double[0]);
	}
	public CompareResult(CodeInfo[] codes, double[] similartys) {
		Assert.notNull(codes, "codes");
		Assert.notNull(similartys, "similartys");
		Assert.isTrue(codes.length == similartys.length, "codes.length == similartys.length");
		this.codes = codes;
		this.similartys = similartys;
	}
	/**
	 * @return codes
	 */
	public CodeInfo[] getCodes() {
		return codes;
	}
	/**
	 * @param codes 要设置的 codes
	 */
	public void setCodes(CodeInfo[] codes) {
		this.codes = codes;
	}
	/**
	 * @return similartys
	 */
	public double[] getSimilartys() {
		return similartys;
	}
	/**
	 * @param similartys 要设置的 similartys
	 */
	public void setSimilartys(double[] similartys) {
		this.similartys = similartys;
	}
	
	/**
	 * 将比较结果以 CodeInfo--similarity 值对的形式返回,顺序与
	 * @return map instance
	 */
	public LinkedHashMap<CodeInfo,Double> asMap(){
		LinkedHashMap<CodeInfo,Double> map = new LinkedHashMap<>();
		for(int i=0;i<codes.length;++i){
			map.put(codes[i], similartys[i]);
		}
		return map;
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + Arrays.hashCode(codes);
		result = prime * result + Arrays.hashCode(similartys);
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (!(obj instanceof CompareResult))
			return false;
		CompareResult other = (CompareResult) obj;
		if (!Arrays.equals(codes, other.codes))
			return false;
		if (!Arrays.equals(similartys, other.similartys))
			return false;
		return true;
	}
	
	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append("CompareResult [codes=");
		builder.append(Arrays.toString(codes));
		builder.append(", similartys=");
		builder.append(Arrays.toString(similartys));
		builder.append("]");
		return builder.toString();
	}
}
